//
//  ZLPictureCell.swift
//  新浪微博
//
//  Created by 朱玉龙 on 9/28/16.
//  Copyright © 2016 朱玉龙. All rights reserved.
//

import UIKit

let cellId = "ZLPictureViewCellId"

class ZLPictureView: UICollectionView {
    
    var pictureInfo: [ZLPicture]? {
        
        didSet {
            
            textLabel.text = "\(pictureInfo?.count ?? 0)"
            
            reloadData()
        }
        
    }
    
    override func awakeFromNib() {
        
        
        dataSource = self
        
        addSubview(textLabel)
        
        textLabel.snp.makeConstraints { (make) in
            
            make.center.equalTo(self.snp.center)
        }
        
        register(ZLPictureCell.self, forCellWithReuseIdentifier: cellId)
        
    }
    
    
    
    private lazy var textLabel:UILabel = {
        
        let l = UILabel(title: "", color: UIColor.white, fontSize: 30)
        
        return l
    }()
    
}
//数据源
extension ZLPictureView: UICollectionViewDataSource {
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return   self.pictureInfo?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ZLPictureCell
        
        cell.pictureInfo = pictureInfo?[indexPath.item]
        
        return cell
    }
}


class ZLPictureCell: UICollectionViewCell {
    
    var pictureInfo:ZLPicture? {
        
        
        didSet {
            let url = URL(string: self.pictureInfo?.wap_pic ?? "")
            
            imageView.sd_setImage(with:url)
            
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(imageView)
        
        imageView.snp.makeConstraints({ (make) in
            
            make.edges.equalTo(self.contentView.snp.edges)
        })
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var imageView:UIImageView =  {
        
        let iv = UIImageView()
        
        iv.contentMode = .scaleAspectFill
        
        iv.clipsToBounds = true
        return iv
    }()
    
}
